const otherConsumeModel = require('../model/otherConsume');
const otherConsumeRecordModel = require('../model/otherConsumeRecord');
const mongoose = require('mongoose');

async function addOtherConsume (req, res, next) {
    try {
        const {
        name,
        img,
        cost,
        payMoney,
        type} = req.body;
        const {userId} = req.user;

        const record = await otherConsumeRecordModel.create({  // 添加记录
            name,
            img,
            cost,
            payMoney,
            type,
            author: mongoose.Types.ObjectId(userId)
        })

        const data = await otherConsumeModel.create({
            name,
            img,
            cost,
            payMoney,
            type,
            author: mongoose.Types.ObjectId(userId),
            record: record._id
        })

        await record.updateOne({$set: {origin: data._id}})

        res.json({
            code: 200,
            msg: '添加消费成功'
        })
    } catch(err) {
        next(err)
    }
}

async function getOtherConsume (req, res, next) {
    try {
        const data = await otherConsumeModel.find();

        res.json({
            code: 200,
            data
        })
    } catch(err) {
        next(err)
    }
}

module.exports = {
    addOtherConsume,
    getOtherConsume
}
